package com.tiger.cloud.controller.crm;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.model.crm.UserSummaryEvents;
import com.tiger.cloud.service.crm.IUserSummaryEventsService;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 要事纪要
 */
@Controller
@RequestMapping(value = "userSummaryEvents")
@Api(value = "要事纪要",tags = "要事纪要")
public class UserSummaryEventsController extends BaseController {

	private static final Logger logger=Logger.getLogger(UserSummaryEventsController.class);
	
	@Autowired
	private IUserSummaryEventsService userSummaryEventsService;
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "分页获取要事纪要",notes = "分页获取要事纪要")
	public Response find(@CurrentUserId Long userId,@RequestBody UserSummaryEvents userSummaryEvents){
		userSummaryEvents.setUserId(userId);
		Page<UserSummaryEvents> datas=userSummaryEventsService.page(userSummaryEvents);
		return ResponseUtil.ok(datas);
	}
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "获取全部要事纪要",notes = "获取全部要事纪要")
	public Response list(@CurrentUserId Long userId){
		UserSummaryEvents userSummaryEvents=new UserSummaryEvents();
		userSummaryEvents.setIsDelete(DeleteEnum.NOMAIL);
		userSummaryEvents.setUserId(userId);
		return ResponseUtil.ok(userSummaryEventsService.find(userSummaryEvents));
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新单个要事纪要",notes = "更新单个要事纪要")
	public Response update(@CurrentUserId Long userId,@RequestBody UserSummaryEvents userSummaryEvents){
		if(userSummaryEvents.getId()!=null&&userSummaryEvents.getId()>0){
			UserSummaryEvents updateEntity=userSummaryEventsService.get(userSummaryEvents.getId());
			if(updateEntity==null||updateEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			userSummaryEvents.setUserId(userId);
			if(userSummaryEventsService.update(userSummaryEvents)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加要事纪要",notes = "添加要事纪要")
	public Response add(@CurrentUserId Long userId,@RequestBody UserSummaryEvents userSummaryEvents){
		userSummaryEvents.setUserId(userId);
		boolean success=userSummaryEventsService.insert(userSummaryEvents);
		return ResponseUtil.ok(success);
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除要事纪要",notes = "删除要事纪要")
	public Response delete(@CurrentUserId Long userId,@RequestBody UserSummaryEvents userSummaryEvents){
		if(userSummaryEvents.getId()!=null&&userSummaryEvents.getId()>0){
			UserSummaryEvents deleteEntity=userSummaryEventsService.get(userSummaryEvents.getId());
			if(deleteEntity==null||deleteEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			userSummaryEvents.setUserId(userId);
			userSummaryEvents.setIsDelete(DeleteEnum.DELETE);
			if(userSummaryEventsService.update(userSummaryEvents)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}

}